import 'dart:async';

import 'package:flutter/material.dart';

class SnackHeadTipsWidget extends StatefulWidget {
  final String tipsText;
  SnackHeadTipsWidget({Key key, @required this.tipsText}) : super(key: key);
  @override
  State<StatefulWidget> createState() => _SnackHeadTipsWidgetState();

  static openSnackHeadTipsWidget(context, String tipsText) {
    Navigator.of(context).push(PageRouteBuilder(
      transitionsBuilder: (_, Animation<double> animation, __, Widget child) {
        return FadeTransition(
          opacity: animation,
          child: SlideTransition(
            position: Tween<Offset>(begin: Offset(0.0, -0.3), end: Offset.zero)
                .animate(animation),
            child: child,
          ),
        );
      },
      opaque: false,
      pageBuilder: (BuildContext context, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        return SnackHeadTipsWidget(
          tipsText: tipsText,
        );
      },
    ));
  }
}

class _SnackHeadTipsWidgetState extends State<SnackHeadTipsWidget> {
  Timer _timer;
  @override
  void initState() {
    super.initState();
    _startTimer();
  }

  _startTimer() {
    _timer = Timer(Duration(seconds: 2), () {
      Navigator.of(context).pop(true);
    });
  }

  void _cancelTimer() {
    _timer?.cancel();
  }

  @override
  Widget build(Object context) {
    return GestureDetector(
      onTap: () {
        _cancelTimer();
        Navigator.of(context).pop();
      },
      child: Column(
        children: <Widget>[
          Container(
            width: double.infinity,
            height: 85.0,
            color: Colors.black54,
            padding: EdgeInsets.only(bottom: 7.0),
            child: Align(
              alignment: Alignment.bottomCenter,
              child: Material(
                color: Colors.transparent,
                child: Text(
                  widget.tipsText,
                  overflow: TextOverflow.ellipsis,
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.white, fontSize: 16.0),
                ),
              ),
            ),
          ),
          Container(
            height: 3.0,
            color: Color(0xFF4A4A4A),
          ),
          Opacity(
            opacity: 0.5,
            child: Container(
              height: 4.0,
              color: Color(0xFFCCCCCC),
            ),
          ),
        ],
      ),
    );
  }
}
